#include "EventLoopThreadPool.h"

EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop) :
    baseLoop_(baseLoop),
    numThreads_(0),
    nextLoopIdx(0)
{

}

void EventLoopThreadPool::start() {
    for (int i = 0; i < numThreads_; i++) {
        EventLoopThread* eventLoopThread = new EventLoopThread();
        threads_.push_back(std::unique_ptr<EventLoopThread>(eventLoopThread));
        loops_.push_back(eventLoopThread->startLoop());
    }
}

EventLoop* EventLoopThreadPool::getNextLoop() {
    if (numThreads_ == 0) {
        return baseLoop_;
    } else {
        EventLoop* loop = loops_[nextLoopIdx];
        nextLoopIdx = (nextLoopIdx + 1) % numThreads_;
        return loop;
    }
}